
<%@page import="storeMovie.JavaBean.AccountBank"%>
<%@page import="storeMovie.Util.AccountBankManager"%>
<%@page import="storeMovie.Util.OrderDetailManager"%>
<%@page import="storeMovie.JavaBean.OrderDetail"%>
<%@page import="storeMovie.Util.OrderManager"%>
<%@page import="storeMovie.JavaBean.Order"%>
<%@page import="storeMovie.Util.MovieManager"%>
<%@page import="storeMovie.JavaBean.Movie"%>
<%@page import="java.util.List"%>
<%@page import="java.text.*"%>
<%@page import="java.util.Date"%>

<%@page import="java.util.Vector"%>
<%@page import="java.sql.*"%><?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%
	MovieManager manager = new MovieManager();
	List<Movie> listMovie = manager.getMovie();
%>
<%
	try {
		Vector<Integer> cart = (Vector<Integer>) session
				.getAttribute("shopingcart");
		Vector<Integer> cartCount = (Vector<Integer>) session
				.getAttribute("cartCount");
		String userName = (String) session.getAttribute("userName");
		//insert into Order table
		int totalCost = 0;
		for (int i = 0; i < listMovie.size(); i++) {
			Movie movie = listMovie.get(i);
			if (cart.contains(new Integer(movie.getMovieId()))) {
				int index = cart
						.indexOf(new Integer(movie.getMovieId()));
				int price = movie.getMoviePrice();
				int total = cartCount.get(index) * price;
				totalCost += total;
			}
		}
		Order order = new Order();
		order.setUserName(userName);
		order.setTotal(totalCost);
		DateFormat dateFormat = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		String datetime = dateFormat.format(date);
		order.setDateOrder(datetime);
		OrderManager orderManager = new OrderManager();
		orderManager.saveOrder(order);

		//==========================update user money========================
		AccountBankManager accountBankManager = new AccountBankManager();
		AccountBank accountBank = accountBankManager
				.getAccountBankByName(userName);
		accountBank.setTotalMoney(accountBank.getTotalMoney()-totalCost);
		accountBankManager.updateAccountBank(accountBank);
		
		int maxId = orderManager.getMaxOrderId(userName);
		System.out.println("Max order details :       " + maxId);
		//=============================insert to order details=================

		//rsMovie.first();
		for (int i = 0; i < listMovie.size(); i++) {
			Movie movie = listMovie.get(i);
			if (cart.contains(new Integer(movie.getMovieId()))) {
				int index = cart
						.indexOf(new Integer(movie.getMovieId()));
				int price = movie.getMoviePrice();
				int total = cartCount.get(index) * price;

				int movie_id = cart.get(index);
				int quantity = cartCount.get(index);
				OrderDetail orderDetail = new OrderDetail();
				orderDetail.setMovieId(movie_id);
				orderDetail.setOrderId(maxId);
				orderDetail.setQuantity(quantity);
				orderDetail.setTotalCost(total);

				OrderDetailManager orderDetailManager = new OrderDetailManager();
				orderDetailManager.saveOrderDetail(orderDetail);
				
				int totalSell = movie.getMovieTotalSell();
				totalSell += quantity;
				movie.setMovieTotalSell(totalSell);
				manager.updateMovie(movie);
			}
		}
		out
				.println("<h1 style='text-align: center;font-size: x-large;'>Purchase successfully </h1>");
		out
				.println("<h2 style='text-align: center;font-size: x-large;'>Thanks you and see you again</h2>");

		cart.removeAllElements();
		cartCount.removeAllElements();
	} catch (Exception ex) {
		out.println("<p>order insert error</p>");
		ex.printStackTrace();
	}
%>